package com.ivi.car.module.carstate.view.activity;

import android.content.Intent;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ivi.car.R;
import com.ivi.car.base.BaseActivity;
import com.ivi.car.bean.carstate.CarPartState;
import com.ivi.car.module.carstate.model.IPartStateContract;
import com.ivi.car.module.carstate.presenter.PartStatePresenter;
import com.wm.machine.baselibrary.common.widget.layout.TitleBar;
import com.wm.machine.baselibrary.utils.LogUtils;

import butterknife.BindView;

/**
 * Copyright
 * author chengxp
 * date 2018/3/12
 * describe 显示配件的具体使用情况页面
 * history
 */

public class PartStateActivity extends BaseActivity implements IPartStateContract.View {
    @BindView(R.id.title)
    TitleBar mTitle;
    @BindView(R.id.tv_mileage_txt)
    TextView mTvMileageTxt;
    @BindView(R.id.tv_mileage_num)
    TextView mTvMileageNum;
    @BindView(R.id.tv_total_mileage_txt)
    TextView mTvTotalMileageTxt;
    @BindView(R.id.tv_total_mileage_num)
    TextView mTvTotalMileageNum;
    @BindView(R.id.pb_mileage)
    ProgressBar mPbMileage;
    @BindView(R.id.tv_time_txt)
    TextView mTvTimeTxt;
    @BindView(R.id.tv_time_num)
    TextView mTvTimeNum;
    @BindView(R.id.tv_total_time_txt)
    TextView mTvTotalTimeTxt;
    @BindView(R.id.tv_total_time_num)
    TextView mTvTotalTimeNum;
    @BindView(R.id.pb_time)
    ProgressBar mPbTime;
    @BindView(R.id.tv_tips)
    TextView mTvTips;
    private PartStatePresenter mStatePresenter;
    private CarPartState mPartState;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_part_state_detial;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        super.initListener();
        mTitle.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        mStatePresenter = new PartStatePresenter(this, mLifecycleProvider);
        Intent intent = getIntent();
        mPartState = (CarPartState) intent.getSerializableExtra("PartState");
        LogUtils.e(mPartState.toString());
        mStatePresenter.setTitle(mPartState);
        mStatePresenter.showUseTime(mPartState);
        mStatePresenter.showMileageDetial(mPartState);
        mStatePresenter.showTips(mPartState);
    }

    @Override
    public void showMileageDetial(CarPartState state) {
        mTvMileageNum.setText(state.getMileage() + "公里");
        mTvTotalMileageNum.setText(state.getTotalMileage() + "");
        ClipDrawable drawable = null;
        if (state.getMileage() >= state.getTotalMileage()) {
            mTvMileageTxt.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));
            mTvMileageNum.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));
            drawable = new ClipDrawable(new ColorDrawable(ContextCompat.getColor(getApplicationContext(),
                    R.color.red)), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        } else {
            mTvMileageTxt.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.green));
            mTvMileageNum.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.green));
            drawable = new ClipDrawable(new ColorDrawable(ContextCompat.getColor(getApplicationContext(),
                    R.color.green)), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        }
        //必须先设置到progressbar上再设置level，告诉这个drawable的宽度有多宽，这个level才能生效
        mPbMileage.setProgressDrawable(drawable);
        drawable.setLevel((state.getTime()) * 100);
        mPbMileage.setMax(state.getTotalMileage());
        mPbMileage.setProgress(state.getMileage());

    }

    @Override
    public void showUseTime(CarPartState state) {
        mTvTotalTimeNum.setText(state.getTotalTime() + "天");
        mTvTimeNum.setText(state.getTime() + "天");
        ClipDrawable drawable = null;
        if (state.getTime() >= state.getTotalTime()) {
            mTvTimeTxt.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));
            mTvTimeNum.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));
            drawable = new ClipDrawable(new ColorDrawable(ContextCompat.getColor(getApplicationContext(),
                    R.color.red)), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        } else {
            mTvTimeTxt.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.green));
            mTvTimeNum.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.green));
            drawable = new ClipDrawable(new ColorDrawable(ContextCompat.getColor(getApplicationContext(),
                    R.color.green)), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        }
        //必须先设置到progressbar上再设置level，告诉这个drawable的宽度有多宽，这个level才能生效
        mPbTime.setProgressDrawable(drawable);
        drawable.setLevel((state.getTime()) * 100);
        mPbTime.setMax(state.getTotalTime());
        mPbTime.setProgress(state.getTime());

    }

    @Override
    public void showTips(CarPartState state) {
        mTvTips.setText("根据驾驶习惯。。。。。。。。。。。。。。。。。。。。");
    }

    public void setTitle(CarPartState state) {
        mTitle.setTitle(state.getName());
    }

}
